# -*- encoding:utf-8 -*-

from datetimestub import datetime, datetime_orig

from django.test import TestCase
from ajax import AppStore,GpStore,AmazonStore
from app_info.models import Company,App,AppVersion,CompanyGroup,Studio
from django.contrib.auth.models import User,Group
from django.contrib.auth import authenticate, login
from qa_info.models import DevQAInfo,ReleaseQAInfo
from sub_info.models import SubInfo
from django.test import Client
import json

from datetime import date, datetime
import ajax
ajax.datetime=datetime

class Wrap(object):
    pass


class PS(object):
    _subs=[]
    def __init__(self,url):
        pass
    
    @classmethod
    def append(cls,sub):
        cls._subs.append(sub)

    def get_released_sub(self):
        return self._subs

    def get_pending_review_sub(self):
        return self._subs       

    def get_pending_release_sub(self):
        return self._subs

    def get_rejected_sub(self):
        return self._subs
        
ajax.PS=PS

class WeekReportTest(TestCase):

    def test_weekreport(self):
        app=AppStore('','')
        app.get_pending_release()

class KanbanTest(TestCase):
    def setUp(self):
        self.user=User.objects.create_user('heyulin','heyulin@game.com','12345')
        self.group=Group.objects.create(name='admin_cd')
        self.g2=Group.objects.create(name='admin_nj')
        self.user.groups.add(self.group)
        self.user.groups.add(self.g2)
        
        self.company=Company.objects.create(name='Cheng Du',display_name='Cheng Du moki',location='CD',code='CD_MOKI')
        self.company2=Company.objects.create(name='nan jing moki',display_name='nan jing moki',location='NJ',code='NJ_MOKI')
        
        CompanyGroup.objects.create(group=self.group,company=self.company)
        CompanyGroup.objects.create(group=self.g2,company=self.company2)
        
        self.studio=Studio.objects.create(pub_name='test studio')
    
        self.setup_apple_model()
        self.setup_gp_model()
        self.setup_amazon_model()
        
        datetime.set_now(datetime_orig.datetime(2016, 5, 30))
    
    def set_common_model(self,obj):
        obj.app=App.objects.create(company=self.company,studio=self.studio,app_store=obj.store,prj_code=obj.name,prd_id=obj.name)
        obj.v0=AppVersion.objects.create(app=obj.app,status='I',code='1.0')
    
        obj.v1=AppVersion.objects.create(app=obj.app,code='1.1')
        obj.dev1 = DevQAInfo.objects.create(app=obj.app,version='1.1',status='P',request_id='v1'+obj.name)
    
        obj.v2=AppVersion.objects.create(app=obj.app,code='1.2')
        obj.dev2=DevQAInfo.objects.create(app=obj.app,version='1.2',status='I',request_id='v2'+obj.name)
    
        obj.v3=AppVersion.objects.create(app=obj.app,code='1.3')
        obj.dev3=DevQAInfo.objects.create(app=obj.app,version='1.3',status='D',request_id='v3'+obj.name)
    
        obj.v4= AppVersion.objects.create(app=obj.app,code='1.4')
        obj.dev4=DevQAInfo.objects.create(app=obj.app,version='1.4',status='D',request_id='v4'+obj.name)
        obj.sub0=SubInfo.objects.create(request_id='rq0'+obj.name,app=obj.app,version='1.4',app_submit_date='2016-04-05')
        obj.rel0=ReleaseQAInfo.objects.create(dev_qa=obj.dev4,status='P',sub_info=obj.sub0)
    
        obj.v5= AppVersion.objects.create(app=obj.app,code='1.5')
        obj.dev5=DevQAInfo.objects.create(app=obj.app,version='1.5',status='D',request_id='v5'+obj.name)
        obj.sub1=SubInfo.objects.create(request_id='rq1'+obj.name,app=obj.app,version='1.5',app_submit_date='2016-04-05')
        obj.rel1=ReleaseQAInfo.objects.create(dev_qa=obj.dev5,status='I',sub_info=obj.sub1)
    
        obj.v6= AppVersion.objects.create(app=obj.app,code='1.6')
        obj.dev6=DevQAInfo.objects.create(app=obj.app,version='1.6',status='D',request_id='v6'+obj.name)
        obj.sub2=SubInfo.objects.create(request_id='rq2'+obj.name,app=obj.app,version='1.6',app_submit_date='2016-04-05')
        obj.rel2=ReleaseQAInfo.objects.create(dev_qa=obj.dev6,status='D',sub_info=obj.sub2)   
    
        obj.v7= AppVersion.objects.create(app=obj.app,code='1.7')
        SubInfo.objects.create(request_id='rq10'+obj.name,app=obj.app,version='1.7',status='Q',app_submit_date='2016-04-05')
        

    def setup_apple_model(self):
        """
        APP_STORE = (
        ('iTunes App Store', u'iTunes App Store'),
        ('Google Play', u'Google Play'),
        ('Amazon App Store', u'Amazon App Store'),
        )
        """
        self.apple=Wrap()
        self.apple.name='apple'
        self.apple.store=u'iTunes App Store'
        self.set_common_model(self.apple)
        
        self.apple.v20= AppVersion.objects.create(app=self.apple.app,code='2.0')
        submitted=SubInfo.objects.create(request_id='rq100',app=self.apple.app,version='2.0',status='S',app_submit_date='2016-04-05')
        
        PS.append(submitted)
        # self.apple.v21= AppVersion.objects.create(app=self.apple.app,code='2.1')
        # self.apple.rej=SubInfo.objects.create(request_id='rq101',app=self.apple.app,version='2.1',status='S',app_submit_date='2016-04-05')
    def setup_gp_model(self):
        self.gp=Wrap()
        self.gp.name='gp'
        self.gp.store='Google Play'
        self.set_common_model(self.gp)
        
        self.gp.v8=AppVersion.objects.create(app=self.gp.app,code='1.8')
        submitted=SubInfo.objects.create(request_id='rq11'+self.gp.name,app=self.gp.app,version='1.8',status='S',review_status='B',app_submit_date='2016-04-05')
        PS.append(submitted)
        
    def setup_amazon_model(self):
        self.amazon=Wrap()
        self.amazon.name='amazon'
        self.amazon.store='Amazon App Store'
        self.set_common_model(self.amazon)
    
    def test_app_store(self):
        # 苹果
        app_store=AppStore(self.user, self.company)
        self.assertEqual(app_store.get_in_productions()[0],self.apple.v0)
        self.assertEqual(app_store.get_pending_dev_qa()[0],self.apple.v1)
        self.assertEqual(app_store.get_in_dev_qa()[0],self.apple.v2)
        self.assertEqual(app_store.get_dev_cleared()[0],self.apple.v3)
        self.assertEqual(app_store.get_pending_rel_qa()[0],self.apple.v4)
        self.assertEqual(app_store.get_in_rel_qa()[0],self.apple.v5)
        self.assertEqual(app_store.get_rel_cleared()[0],self.apple.v6)
        self.assertEqual(app_store.get_queued()[0],self.apple.v7)
        
            # 测试与ps对接
        self.assertEqual(app_store.get_submitted()[0],self.apple.v20)
        self.assertEqual(app_store.get_rejected()[0],self.apple.v20)
        self.assertEqual(app_store.get_pending_release()[0],self.apple.v20)
        self.assertEqual(app_store.get_released()[0],self.apple.v20)
        
        # google
        gp_store=GpStore(self.user, self.company)
        self.assertEqual(gp_store.get_in_productions()[0],self.gp.v0)
        self.assertEqual(gp_store.get_pending_dev_qa()[0],self.gp.v1)
        self.assertEqual(gp_store.get_in_dev_qa()[0],self.gp.v2)
        self.assertEqual(gp_store.get_dev_cleared()[0],self.gp.v3)
        self.assertEqual(gp_store.get_pending_rel_qa()[0],self.gp.v4)
        self.assertEqual(gp_store.get_in_rel_qa()[0],self.gp.v5)
        self.assertEqual(gp_store.get_rel_cleared()[0],self.gp.v6)
        self.assertEqual(gp_store.get_queued()[0],self.gp.v7)
        
        self.assertEqual(gp_store.get_submitted()[0],self.gp.v8)
        
        
        # 亚马逊
        z_store=AmazonStore(self.user, self.company)
        self.assertEqual(z_store.get_in_productions()[0],self.amazon.v0)
        self.assertEqual(z_store.get_pending_dev_qa()[0],self.amazon.v1)
        self.assertEqual(z_store.get_in_dev_qa()[0],self.amazon.v2)
        self.assertEqual(z_store.get_dev_cleared()[0],self.amazon.v3)
        self.assertEqual(z_store.get_pending_rel_qa()[0],self.amazon.v4)
        self.assertEqual(z_store.get_in_rel_qa()[0],self.amazon.v5)
        self.assertEqual(z_store.get_rel_cleared()[0],self.amazon.v6)
        self.assertEqual(z_store.get_queued()[0],self.amazon.v7)        
    

    def test_company(self):
        c=Client()
        c.login(username='heyulin',password='12345')
        data={'get_companys':{}}
        rt=c.post('/production_kanban/ajax/', data=json.dumps(data),content_type='application/json')
        rt=json.loads(rt.content)
        print (rt.get('get_companys').get('companys'))
       
    
    def test_other_company(self):
            
        app_store=AppStore(self.user, self.company2)
        self.assertEqual(app_store.get_in_productions(),[])
        self.assertEqual(app_store.get_pending_dev_qa(),[])
        self.assertEqual(app_store.get_in_dev_qa(),[])
        self.assertEqual(app_store.get_dev_cleared(),[])
        self.assertEqual(app_store.get_pending_rel_qa(),[])
        self.assertEqual(app_store.get_in_rel_qa(),[])
        self.assertEqual(app_store.get_rel_cleared(),[])
        self.assertEqual(app_store.get_queued(),[])        
        
        # google
        gp_store=GpStore(self.user, self.company2)
        self.assertEqual(gp_store.get_in_productions(),[])
        self.assertEqual(gp_store.get_pending_dev_qa(),[])
        self.assertEqual(gp_store.get_in_dev_qa(),[])
        self.assertEqual(gp_store.get_dev_cleared(),[])
        self.assertEqual(gp_store.get_pending_rel_qa(),[])
        self.assertEqual(gp_store.get_in_rel_qa(),[])
        self.assertEqual(gp_store.get_rel_cleared(),[])
        self.assertEqual(gp_store.get_queued(),[])
        
        self.assertEqual(gp_store.get_submitted(),[])        
    
    
    def test_get_data(self):
        app_store=AppStore(self.user, self.company)
        print app_store.get_data()
        
        gp_store=GpStore(self.user, self.company)
        print gp_store.get_data()